package com.benew.ntt.evaluate.sdk

import android.os.Handler
import android.os.Looper
import android.os.Message

class EvalSDKHandler(looper: Looper, callback: Callback) : Handler(looper, callback) {
    /**
     * 录音中
     */
    fun recordOfByte(data: ByteArray, begin: Int, end: Int, decibel: Double) {
        val msg = Message()
        msg.what = EvalSDKWhat.RECORDING
        msg.obj = arrayOf(data, begin, end, decibel)

        dispatchMessage(msg)
    }

    /**
     * 开始评测
     */
    fun sendStartEval(entity: EvalSDKEntity) {
        val msg = Message.obtain()
        msg.what = EvalSDKWhat.START_EVAL
        msg.obj = entity
        sendMessage(msg)
    }

    /**
     * 评测错误
     */
    fun sendError(error: String?, code: Int?) {
        //关闭超时倒计时
        sendCloseTimeOutCountDown()
        sendRecordCloseTimeOutCountDown()
        val msg = Message.obtain()
        msg.what = EvalSDKWhat.ERROR
        msg.obj = error
        msg.arg1 = code?:0
        sendMessage(msg)
    }

    /**
     * 开始录音
     */
    fun sendStartRecord() {
        sendEmptyMessage(EvalSDKWhat.START_RECORD)
    }

    /**
     * 评测进度结果
     */
    fun sendEvalProgress(message: String?) {
        val msg = Message.obtain()
        msg.what = EvalSDKWhat.PROGRESS
        msg.obj = message
        sendMessage(msg)
    }

    /**
     * 录音结束
     */
    fun sendRecordEnd() {
        sendRecordCloseTimeOutCountDown()
        sendEmptyMessage(EvalSDKWhat.RECORD_END)
    }

    /**
     * 评测结果
     */
    fun sendResult(message: String?) {
        //关闭超时倒计时
        sendCloseTimeOutCountDown()
        sendRecordCloseTimeOutCountDown()

        val msg = Message.obtain()
        msg.what = EvalSDKWhat.RESULT
        msg.obj = message
        sendMessage(msg)

    }

    /**
     * 评测关闭
     */
    fun sendEvalClose() {
        //关闭超时倒计时
        sendCloseTimeOutCountDown()
        sendRecordCloseTimeOutCountDown()

        sendEmptyMessage(EvalSDKWhat.EVAL_CLOSE)
    }

    /**
     * 录音文件
     */
    fun sendRecordFile(path: String?) {
        sendRecordCloseTimeOutCountDown()

        val msg = Message.obtain()
        msg.what = EvalSDKWhat.RECORD_FILE
        msg.obj = path
        sendMessage(msg)
    }

    /**
     * 发送超时倒计时
     */
    fun sendTimeOutCountDown(delayMillis: Long) {
        removeMessages(EvalSDKWhat.TIMEOUT)
        val msg = Message.obtain()
        msg.what = EvalSDKWhat.TIMEOUT
        sendMessageDelayed(msg, delayMillis)
    }

    /**
     * 立刻发送超时
     */
    fun sendTimeOut() {
        removeMessages(EvalSDKWhat.TIMEOUT)
        val msg = Message.obtain()
        msg.what = EvalSDKWhat.TIMEOUT
        sendMessage(msg)
    }

    /**
     * 发送取消超时倒计时
     */
    fun sendCloseTimeOutCountDown() {
        removeMessages(EvalSDKWhat.TIMEOUT)
    }

    /**
     * 发送取消评测
     */
    fun sendCancel() {
        removeCallbacksAndMessages(null)
        sendEmptyMessage(EvalSDKWhat.CANCEL)
    }

    /**
     * 发送录音超时倒计时
     */
    fun sendRecordTimeOutCountDown(delayMillis: Long) {
        removeMessages(EvalSDKWhat.RECORD_TIMEOUT)
        val msg = Message.obtain()
        msg.what = EvalSDKWhat.RECORD_TIMEOUT
        sendMessageDelayed(msg, delayMillis)
    }

    /**
     * 发送取消录音超时倒计时
     */
    fun sendRecordCloseTimeOutCountDown() {
        removeMessages(EvalSDKWhat.RECORD_TIMEOUT)
    }

}